package cn.wizzer.app.web.commons.ext.websocket.action.msg.attendant;

import cn.wizzer.app.user.modules.models.vo.ChatUserVO;
import cn.wizzer.app.web.commons.ext.websocket.filter.AttendantFilter;
import cn.wizzer.app.web.commons.ext.websocket.filter.TokenFilter;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.base.NettyCache;
import cn.wizzer.app.web.commons.ext.websocket.main.result.ChatResult;
import cn.wizzer.app.web.commons.utils.NettyMsgUtil;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.util.JSONUtil;
import cn.wizzer.framework.util.param.Param;
import cn.wizzer.framework.util.param.Param4Map;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import org.nutz.ioc.loader.annotation.IocBean;

/**
 * @author zl           客服发起会话
 * @date 2020/7/1 9:42
 */
@IocBean
@NettyAction(uri = "/attendant/start", filters = {TokenFilter.class, AttendantFilter.class})
public class AttendantStartAction extends BaseNettyAction {

    @Override
    public Result action(Channel income, @Param4Map(fields = {@Param(name = "target",required = true)}) JSONObject params) {
        String target = params.getString("target");
        ChatUserVO self = NettyCache.globalGet(params.get("appid"));
        if(!self.getUsers().contains(target)){
            self.getUsers().add(target);
        }
        ChatUserVO targetUser = NettyCache.globalGet(target);
        if(targetUser != null){
            NettyMsgUtil.send(targetUser.getChannel(), ChatResult.MessageTypeEnum.ACTION, ChatResult.ActionEnum.JOIN, self, null);
            //缓存指定客服
            NettyCache.localSet(targetUser.getChannel(),"targetAttendant", self.getId());
        }
        return Result.success();
    }
}
